﻿using EricOulashin;
using NAudio.Wave;
using pHMb.Spectrogram;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Lung_Sounds_V2
{
    /// <summary>
    /// Interaction logic for ProcessingWaveWindow.xaml
    /// </summary>
    public partial class ProcessingWaveWindow : Window
    {
        public ProcessingWaveWindow(SpectrogramPane spectrogramPane, RealTimeAudioProcesser processor)
        {
            _spectrogramPane = spectrogramPane;
            _processor = processor;
            InitializeComponent();
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private WAVFile _wavOpener;
        private WAVFile _wavSaver;
        private SpectrogramPane _spectrogramPane;
        private RealTimeAudioProcesser _processor;

        public bool LoadWave(string fileName, int windowSize = 4096)
        {
            if (_wavOpener != null)
            {
                _wavOpener.Close();
            }

            if (_wavSaver != null)
            {
                _wavSaver.Close();
            }

            try
            {
                _wavOpener = new WAVFile();
                _wavOpener.Open(fileName, WAVFile.WAVFileMode.READ);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Error opening wave file\n{0}", ex), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            try
            {
                //_spectrogramPane.Dock = DockStyle.Left;

                _spectrogramPane.WindowSamples = windowSize;

                int sampleId = 0;
                double[] buffer = new double[windowSize];
                while (true)
                {
                    if (sampleId == windowSize)
                    {
                        Lung_Sounds_V2.RealTimeAudioProcesser.FFTResult result = _processor.PerformFFT(buffer);
                        _spectrogramPane.Windows.Add(result.FFT);
                        sampleId = 0;
                    }

                    if (_wavOpener.FilePosition < _wavOpener.FileSizeBytes)
                    {
                        buffer[sampleId] = (double)_wavOpener.GetNextSampleAs16Bit();
                        sampleId++;
                    }
                    else
                    {
                        break;
                    }
                }

                _spectrogramPane.UpdateGraph();
                _processor.StartSelect(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Error reading wave file\n{0}", ex), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }

            return true;
        }
    }
}
